package com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.provider

import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.request.RequestOptions
import com.example.library_common.adapter.JssBaseQuickAdapter
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.adapter.multi.JssMultiItemViewHolder
import com.example.library_common.utils.QMUIDisplayHelper
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleAdvertiseEntity
import com.example.shushiyunsst.fragment.main.home.child.new_sale.recycler.bean.NewSaleAdvertiseWrapperEntity
import com.example.wallet.global.AppGlobals
import jp.wasabeef.glide.transformations.RoundedCornersTransformation

class NewSaleAdvertiseProvider(view: View?) :
    JssMultiItemViewHolder<NewSaleAdvertiseWrapperEntity>(view) {

    private var mAdapter: JssBaseQuickAdapter<NewSaleAdvertiseEntity>? = null

    private val pagerSnapHelper = PagerSnapHelper()

    private var height = 0

    init {
        val screenWidth = QMUIDisplayHelper.getScreenWidth(AppGlobals.getApplication())
        height = (screenWidth / 3.4).toInt()
    }


    override fun getItemLayout(): Int {
        return R.layout.new_sale_advertise_provider
    }

    override fun convert(context: Context?, m: NewSaleAdvertiseWrapperEntity?) {
        super.convert(context, m)
        val mRecyclerView: RecyclerView = findView22(R.id.mRecyclerView) ?: return
        val lp = mRecyclerView.layoutParams
        if (lp != null && lp.height != height) {
            lp.height = height
            mRecyclerView.layoutParams = lp
        }

//        mRecyclerView.setHasFixedSize(true)
        mRecyclerView.layoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

        if (mAdapter == null) {
            mAdapter =
                object :
                    JssBaseQuickAdapter<NewSaleAdvertiseEntity>(R.layout.new_sale_advertise_item) {


                    override fun convert(helper: JssBaseViewHolder, item: NewSaleAdvertiseEntity) {
                        super.convert(helper, item)
                        val radius = QMUIDisplayHelper.dp2px(context, 6)
                        val mRequestOptions = RequestOptions
                            .bitmapTransform(
                                RoundedCornersTransformation(radius, 0)
                            )
                        helper.setImageNetUrl(
                            R.id.img_cover,
                            item.modUrl,
                            R.drawable.img_erros_bg_rect,
                            mRequestOptions
                        )
                    }
                }
        }

        mRecyclerView.adapter = mAdapter
        mRecyclerView.onFlingListener = null
        mAdapter?.clears()
        mAdapter?.addData(m?.getData() as Collection<NewSaleAdvertiseEntity>)
    }

    override fun onViewAttachedToWindow() {
        super.onViewAttachedToWindow()
        val mRecyclerView: RecyclerView = findView22(R.id.mRecyclerView)
        pagerSnapHelper.attachToRecyclerView(mRecyclerView)
    }
}